import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/HomeView.vue'),
      meta: { title: '首页' }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/auth/LoginView.vue'),
      meta: { title: '登录', requiresGuest: true }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/auth/RegisterView.vue'),
      meta: { title: '注册', requiresGuest: true }
    },

    {
      path: '/articles/:id',
      name: 'article-detail',
      component: () => import('@/views/article/ArticleDetailView.vue'),
      meta: { title: '文章详情' }
    },
    {
      path: '/admin',
      name: 'admin',
      component: () => import('@/layouts/AdminLayout.vue'),
      meta: { title: '管理后台', requiresAuth: true },
      children: [
        {
          path: '',
          name: 'admin-dashboard',
          component: () => import('@/views/admin/DashboardView.vue'),
          meta: { title: '仪表盘', requiresAuth: true }
        },
        {
          path: 'articles',
          name: 'admin-articles',
          component: () => import('@/views/admin/ArticleManageView.vue'),
          meta: { title: '文章管理', requiresAuth: true }
        },
        {
          path: 'articles/create',
          name: 'admin-article-create',
          component: () => import('@/views/admin/ArticleEditView.vue'),
          meta: { title: '创建文章', requiresAuth: true }
        },
        {
          path: 'articles/:id/edit',
          name: 'admin-article-edit',
          component: () => import('@/views/admin/ArticleEditView.vue'),
          meta: { title: '编辑文章', requiresAuth: true }
        },
        {
          path: 'profile',
          name: 'admin-profile',
          component: () => import('@/views/admin/ProfileView.vue'),
          meta: { title: '个人资料', requiresAuth: true }
        },
        {
          path: 'tests',
          name: 'admin-tests',
          component: () => import('@/views/admin/TestManageView.vue'),
          meta: { title: '测试管理', requiresAuth: true }
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('@/views/NotFoundView.vue'),
      meta: { title: '页面不存在' }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 博客管理系统`
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth && !authStore.isLoggedIn) {
    ElMessage.warning('请先登录')
    next({ name: 'login', query: { redirect: to.fullPath } })
    return
  }
  
  // 检查是否需要游客状态（已登录用户不能访问登录/注册页）
  if (to.meta.requiresGuest && authStore.isLoggedIn) {
    next({ name: 'home' })
    return
  }
  
  next()
})

export default router
